import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class Week04HiveMain {


    public static void statementExecute(String sql, Statement statement) {
        try {
            ResultSet resultSet = statement.executeQuery(sql);
            if(resultSet != null) {
                List<String> columnNameList = new ArrayList<String>();
                ResultSetMetaData resultSetMetaData = resultSet.getMetaData();
                int columnNum = resultSetMetaData.getColumnCount();
                for(int size = 1; size <= columnNum; size++) {
                    String columnName = resultSetMetaData.getColumnName(size);
                    columnNameList.add(columnName);
                }

                int lineSize = 1;
                System.out.print(" lineSize:" + lineSize);
                for(String columnName : columnNameList) {
                    String columnValue = resultSet.getString(columnName);
                    System.out.print(" " + columnName + ": " + columnValue);
                }
                System.out.println();
            }
        } catch(Exception e) {
            e.printStackTrace();
        }
    }
    
    public static void main(String[] args) {
        try {
            String driverName = "org.apache.hive.jdbc.HiveDriver";

            Class.forName(driverName);
            Connection connection = DriverManager.getConnection("jdbc:hive2://127.0.0.1:10000/default", "", "");
            System.out.println("connection " + connection);

            // connection.prepareStatement()
            // Statement
            Statement statement = connection.createStatement();

            // query 1
            String querySql1 = "select * from t_user limit 10";
            statementExecute(querySql1, statement);


        } catch(Exception e) {
            e.printStackTrace();
        }


    }
}
